package com.china08.yxyapi.api.manage.tec.teaching.weikecheng.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;

/**
 * 老师---我的微课 ---编辑页面回显
 * 
 */
@CrossOrigin
@RestController
public class WeikechengUptController extends YxyApiBaseController {

    @Autowired
    private YxyResWeikeKnowRepository weikeKnowRepository;

    @Autowired
    private YxyWeikeService weikeService;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/weikecheng/weike", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "weikeId", required = true) String weikeId, RespModel resp) {

        ElasticYxyResResource weikeResource = weikeService.findOneResource(weikeId);
        resp = new RespModel();
        resp.title = weikeResource.getTitle();
        resp.cover = weikeResource.getCover();
        resp.intro = weikeResource.getIntro();
        resp.weikeId = weikeResource.getId();
        resp.viewScope = weikeResource.getViewScope();
        resp.stage = weikeResource.getStage();
        resp.subject = weikeResource.getWeikeSubject();
        resp.subjectName = weikeResource.getWeikeSubjectName();
        resp.md5 = weikeResource.getMd5();
        List<YxyResWeikeKnow> weikeKnows = weikeKnowRepository.findByWeikeId(weikeId);
        // 关联的知识点
        List<KnowRespModel> knows = new ArrayList<KnowRespModel>();
        for (YxyResWeikeKnow yxyResWeikeKnow : weikeKnows) {
            KnowRespModel knowRespModel = new KnowRespModel();
            knowRespModel.id = yxyResWeikeKnow.getKnowId();
            knowRespModel.name = yxyResWeikeKnow.getKnowName();
            knows.add(knowRespModel);
        }
        resp.knows = knows;
        return resp;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String title;// 标题(即是微课的视频名称)

        public String intro;// 简介

        public String weikeId;// 微课Id

        public String cover;// 封面

        public Integer viewScope;// 共享范围 0:私有,1学校,2,教育云

        public String stage;// 学段

        public String subject;// 科目

        public String subjectName;// 科目名称

        public String md5;

        public List<KnowRespModel> knows;// 已关联的知识点

    }

    protected static class KnowRespModel extends YxyApiRespModel {

        public Integer id;// 三级

        public String name;// 三级知识点名称

    }

}